/*
 * net_packet.h
 *
 *  Created on: 2023年7月22日
 *      Author: mcealt_DS
 */
#ifndef NET_PACKET_H
#define NET_PACKET_H
#include "stdint.h"
#include "mac_transmitter.h"
#include "mac_receiver.h"
#include "bsp_rf.h"
#include "app_key_value.h"

#define xCONFIG_LOG_NET_PACKET   //打印开关
#define xCONFIG_LOG_NET_PACKET_ERROR   //开启所有接收打印 包括错误地址等

#define FRAMECTL_TPMODE_MASK        (0B10000000)
#define FRAMECTL_BURSTMODE_MASK     (0B01000000)
#define FRAMECTL_VERSION_MASK       (0B00000111)
#define FRAMECTL_TPMODE_POS         (7)
#define FRAMECTL_BURSTMODE_POS      (6)
#define FRAMECTL_VERSION_POS        (0)

#define QRFR1_ISRXWAKEON_MASK       (0B001000)
#define QRFR1_ISACKBCAST_MASK       (0B00010000)
#define QRFR1_ISCONFIGREQ_MASK      (0B00001000)
#define QRFR1_ISRXALWAYSON_MASK     (0B00000100)
#define QRFR1_ISSPVNEN_MASK         (0B00000010)
#define QRFR1_ISACKREQ_MASK         (0B00000001)
#define QRFR1_ISRXWAKEON_POS        (5)
#define QRFR1_ISACKBCAST_POS        (4)
#define QRFR1_ISCONFIGREQ_POS       (3)
#define QRFR1_ISRXALWAYSON_POS      (2)
#define QRFR1_ISSPVNEN_POS          (1)
#define QRFR1_ISACKREQ_POS          (0)

#define QRFH1_IOEN_MASK             (0B10000000)
#define QRFH1_ISAUTOPOWER_MASK      (0B00001000)
#define QRFH1_ISRXALWAYSON_MASK     (0B00000100)
#define QRFH1_ISRXWAKE_MASK         (0B00000010)
#define QRFH1_ISACKREQ_MASK         (0B00000001)
#define QRFH1_IOEN_POS              (7)
#define QRFH1_ISAUTOPOWER_POS       (3)
#define QRFH1_ISRXALWAYSON_POS      (2)
#define QRFH1_ISRXWAKE_POS          (1)
#define QRFH1_ISACKREQ_POS          (0)

#define QRFH2_ISMAXPOWER_MASK       (0B00100000)
#define QRFH2_ISRSSI_MASK           (0B00010000)
#define QRFH2_ISFM_MASK             (0B00001000)
#define QRFH2_ISUM_MASK             (0B00000110)
#define QRFH2_ISFILETX_MASK         (0B00000001)
#define QRFH2_ISMAXPOWER_POS        (5)
#define QRFH2_ISRSSI_POS            (4)
#define QRFH2_ISFM_POS              (3)
#define QRFH2_ISUM_POS              (2)
#define QRFH2_ISFILETX_POS          (0)

#define QRF_ISUM_NONE                (0B00)
#define QRF_ISUM_2S                  (0B01)
#define QRF_ISUM_1S                  (0B10)
#define QRF_ISUM_0_5S                (0B11)

#define MSGRETRY_DEF                0xFF

#ifdef RFM_DEVICE
typedef enum
{
  NET_UP_EVENT_ADHOC = 0,
  NET_UP_EVENT_SPVN
}NET_UP_EVENT_TYPE_EN;

//上报物模型结果
typedef int (*netCallBackFun)(NET_UP_EVENT_TYPE_EN type, bool isSucced, int8_t rssi, void *pData);
int reg_net_upload_result(netCallBackFun p);

bool isregeist(void);
#endif

// downlink 失败原因
enum
{
    DOWNLINK_RET_OK = 0,   // 无错误，即HUB下发成功且收到子设备应答
    DOWNLINK_RET_CH_BUSY,  // 下发通道忙
    DOWNLINK_RET_EXIST,    // 当前设备有其他指令正在执行
    DOWNLINK_RET_COMM_FAIL, // HUB通信失败
};

// 组包传参结构体
typedef struct 
{
  /* data */
  uint8_t p2p_ch; // 点对点通信使用的通道号 0-4
  uint8_t retry;  // 重传次数
}net_pack_t;


//保活定时器
void isUmTimerStart(uint32_t ms);

// mac tx回调
void mac_txStatecallbackAdhocUp(uint8_t **ppPacket, MAC_TX_STATE_E currState, void *pData);
void mac_txStatecallbackAdhocDown0(uint8_t **ppPacket, MAC_TX_STATE_E currState, void *pData);
void mac_txStatecallbackAdhocDown1(uint8_t **ppPacket, MAC_TX_STATE_E currState, void *pData);
void mac_txStatecallbackAdhocDown2(uint8_t **ppPacket, MAC_TX_STATE_E currState, void *pData);
void mac_txStatecallbackAdhocDown3(uint8_t **ppPacket, MAC_TX_STATE_E currState, void *pData);
void mac_txStatecallbackAdhocDown4(uint8_t **ppPacket, MAC_TX_STATE_E currState, void *pData);
#if (DOWNLINK_CONCURRENCY_CAPABILITY > 5)
#error "ADD MORE txStatecallbackAdhocDownX define"
#endif
// mac rx回调
bool mac_RxCallback(RF_PACKET_ST *pPacket, MAC_RX_STATE_E currState, void *pData);
//ack回调
void mac_txStatecallbackAck (uint8_t** ppPacket,MAC_TX_STATE_E currState,void* pData);
//心跳回调
void mac_txStatecallbackAdhocSpvn (uint8_t** ppPacket,MAC_TX_STATE_E currState,void* pData);
//广播回调
void mac_txStatecallbackBcast(uint8_t **ppPacket, MAC_TX_STATE_E currState, void *pData);
//广播回调
void mac_txStatecallbackRealTimeBcast(uint8_t **ppPacket, MAC_TX_STATE_E currState, void *pData);

//组包
void net_pack(uint8_t destID, uint8_t *packet, uint8_t cmd, void *pdata);
// 解包
bool net_unpack(uint8_t *packet, uint16_t packet_len);
bool net_unpack_ack(uint8_t *packet, uint16_t packet_len);
//net 初始化
void net_init(void);
//net主循环
void net_while(void);

// 保存物模型 CAT_SOUNDER\CAT_KEYPAD\BTYPE_SWITCH
void save_option(uint8_t btype, uint8_t *bdata, uint8_t len);

//net需要向hub发送事件的处理
void net_to_hub_event(uint8_t *packet, uint16_t packetLen);
//hub向net发送事件的处理
void hub_to_net_event(uint8_t *payload, uint16_t payloadLen);

// 产生广播物模型
app_key_value_err_e set_BD_key_value(const keyValue_t *keyValue);

//设备强制保活
void set_s_updateMeAppEn(bool state);//true 保活 

//打印开关
void set_net_printf_log(bool sw, bool save);

//获取开关状态
bool get_net_printf_log(void);

#endif /*NET_PROTOCOL_H*/
